<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name" placeholder="请输入名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12" class="mb20">
					<el-form-item label="编码" prop="code">
						<el-input v-model="form.code" placeholder="请输入编码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="所属机器人" prop="robotId" label-width="auto">
						<el-select v-model="form.robotId" placeholder="请选择所属机器人" :value-on-clear="null" clearable>
							<el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in robotData" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="里程值" prop="mileage">
						<div class="flex items-center">
							<el-input v-model="form.mileage" placeholder="请输入里程值" />
							<el-button type="primary" icon="Pointer" round class="ml-8" @click="onGetMileage">获取</el-button>
						</div>
					</el-form-item>
				</el-col>
				<el-col :span="16" class="mb20">
					<el-form-item label="检测标准" prop="checkStandardIds">
						<el-select v-model="form.checkStandardIds" placeholder="请选择检测标准" :value-on-clear="null" multiple clearable>
							<template #label="{ label }">
								<span>{{ label }} </span>
							</template>
							<el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in standardData" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="CheckPositionDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, getMileageByRobotId } from '/@/api/config/checkposition';
import { rule } from '/@/utils/validate';
import { fetchList as fetchRobotList } from '/@/api/config/robot';
import { fetchList as fetchStandardList } from '/@/api/config/checkstandard';
const robotData = ref<any[]>([]);
const standardData = ref<any[]>([]);
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	code: '',
	name: '',
	mileage: '',
	robotId: '',
	checkStandardIds: '',
});

// 定义校验规则
const dataRules = ref({
	name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
	mileage: [{ required: true, message: '里程值不能为空', trigger: 'blur' }],
	robotId: [{ required: true, message: '所属机器人', trigger: 'blur' }],
	checkStandardIds: [{ required: true, message: '检测标准不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取checkPosition信息
	if (id) {
		form.id = id;
		getcheckPositionData(id);
	}

	// 获取数据
	getRobotData();
	getStandardData();
};

//
const getRobotData = () => {
	fetchRobotList({ current: 1, size: 100000 }).then((res) => {
		robotData.value = res.data.records;
	});
};

//
const getStandardData = () => {
	fetchStandardList({ current: 1, size: 100000 }).then((res) => {
		standardData.value = res.data.records;
	});
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		if (form.id) {
			visible.value = false;
		}
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

//获取里程值
const onGetMileage = async () => {
	// 校验 robotId 字段
	const valid = await dataFormRef.value.validateField('robotId').catch(() => {});
	if (!valid) {
		useMessage().wraning('请选择所属机器人');
		return;
	}
	//获取里程值
	loading.value = true;
	getMileageByRobotId(form.robotId)
		.then((res: any) => {
			console.info(res.data);
			form.mileage = res.data;
		})
		.finally(() => {
			loading.value = false;
		});
};

// 初始化表单数据
const getcheckPositionData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
